package com.huilian.iotbox.client.service.pay;

import com.huilian.iotbox.client.dao.OrderDao;
import com.huilian.iotbox.client.service.*;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.dto.OrderDto;
import com.huilian.iotbox.data.enums.ErrorMsgEnum;
import com.huilian.iotbox.data.enums.MchTypeEnum;
import com.huilian.iotbox.data.exception.SysException;
import com.huilian.iotbox.data.po.MerchantStore;
import com.huilian.iotbox.data.po.SystemConfig;
import com.huilian.iotbox.data.properties.ConfigProperties;
import com.huilian.iotbox.data.service.SystemConfigService;
import com.huilian.iotbox.data.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;

/**
 * 线下收款码
 */
@Service("offlinePayService")
public class OfflinePayService extends AbstractPayService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    public ConfigProperties configProperties;
    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private MerchantStoreService merchantStoreService;
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Override
    public void verification(OrderDto orderDto, Long memberId) throws ParseException {
        super.verification(orderDto, memberId);
        orderDto.setMchType(MchTypeEnum.OFFLINE_COLLECTION_CODE.getCode());
    }

    @Override
    public void orderGen(OrderDto orderDto, HttpServletRequest request) {

        // 查询门店信息
        String storeKey = String.format(RedisKey.MERCHANT_STORE, orderDto.getMerchantStoreId());
        MerchantStore merchantStore = (MerchantStore) redisCacheTemplate.opsForValue().get(storeKey);
        if (merchantStore == null) {
            MerchantStore store = new MerchantStore();
            store.setId(orderDto.getMerchantStoreId());
            merchantStore = merchantStoreService.findMerchantStore(store);
            if (ObjectUtils.isEmpty(merchantStore)) {
                throw new SysException(ErrorMsgEnum.MERCHANT_STORE_NOT_FOUND.getMsg());
            } else if (ObjectUtils.isEmpty(merchantStore.getMerchantId())) {
                throw new SysException(ErrorMsgEnum.MERCHANT_STORE_NOT_ASSOCIATED_MERCHANT.getMsg());
            }
            redisCacheTemplate.opsForValue().set(storeKey, merchantStore, 1, TimeUnit.HOURS);
        }
        if (StringUtils.isEmpty(merchantStore.getMeasuringUnit())) {
            SystemConfig systemConfig = systemConfigService.findOneSystemConfigById(1);
            merchantStore.setMeasuringUnit(systemConfig.getDefaultMeasuringUnit());
        }

        StringBuffer stringBuffer = new StringBuffer("线下收款码收款：");
        stringBuffer.append(orderDto.getTotalAmount());
        stringBuffer.append("元");
        orderDto.setBody(stringBuffer.toString());
        orderDto.setIp(Util.getIpAddress(request));

        orderDao.add(orderDto);
    }
}
